package com.py.ledger.controller;

import com.py.ledger.domain.InventoryDetails;
import com.py.ledger.service.IInventoryDetailsService;
import com.py.message.MessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Component
public class Time {

    @Autowired
    private IInventoryDetailsService inventoryDetailsService;

    @Resource
    private MessageProducer messageProducer;

    //凌晨一点查一次库存

    @Scheduled(cron = "0 0 0 * * ?")
    public void messageList() {
        System.out.println("进来了吗");
        InventoryDetails inventoryDetails=new InventoryDetails();
        List<InventoryDetails> list = inventoryDetailsService.selectInventoryDetailsList(inventoryDetails);
        System.out.println("list:"+list.size());
        for (InventoryDetails detail : list) {
            Long currentInventory = detail.getCurrentInventory();
            Long inventoryLowerLimit = detail.getInventoryLowerLimit();
            Long inventoryUpperLimit = detail.getInventoryUpperLimit();

            if (currentInventory != null) {
                if (inventoryLowerLimit != null && currentInventory < inventoryLowerLimit) {
                    messageProducer.sendMessageDirect2("库存预警: " + detail.getProductName() + " 库存低于下限");
                    System.out.println(12322);
                } else if (inventoryUpperLimit != null && currentInventory > inventoryUpperLimit) {
                    messageProducer.sendMessageDirect2("库存预警: " + detail.getProductName() + " 库存高于上限");
                    System.out.println(2223);
                }
            }
        }
    }
}
